<template>
  <div class="six-panel-comic">
    <h2>六格漫画生成</h2>

    <div class="input-section">
      <textarea v-model="inputText" placeholder="输入你的故事，我们将生成六格漫画..."></textarea>

      <div class="controls">
        <div class="style-selector">
          <label>漫画风格:</label>
          <select v-model="selectedStyle">
            <option v-for="style in styles" :value="style.value" :key="style.value">
              {{ style.label }}
            </option>
          </select>
        </div>

        <button @click="generateComic" :disabled="isGenerating">
          {{ isGenerating ? '生成中...' : '生成六格漫画' }}
        </button>
      </div>
    </div>

    <div v-if="comicData" class="comic-display">
      <div class="comic-description">
        <h3>故事分镜描述</h3>
        <div class="description-text">
          <p v-for="(scene, index) in comicData.scenes" :key="index">
            <strong>第{{ index+1 }}格:</strong> {{ scene }}
          </p>
        </div>
      </div>

      <div class="comic-panels">
        <div v-for="(url, index) in comicData.image_urls" :key="index" class="panel">
          <div class="panel-number">第{{ index+1 }}格</div>
          <img v-if="url" :src="url" :alt="'漫画第'+(index+1)+'格'">
          <div v-else class="error-panel">生成失败</div>
        </div>
      </div>

      <div class="comic-actions">
        <button @click="downloadAll" class="download-btn">
          <i class="fas fa-download"></i> 下载全部
        </button>
        <button @click="regenerate" class="regenerate-btn">
          <i class="fas fa-sync-alt"></i> 重新生成
        </button>
      </div>
    </div>

    <div v-if="isGenerating" class="generation-progress">
      <div class="progress-bar">
        <div class="progress" :style="{ width: progress + '%' }"></div>
      </div>
      <p>正在生成第{{ currentPanel }}/6格漫画...</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      inputText: '',
      selectedStyle: 'default',
      styles: [
        { value: 'default', label: '默认风格' },
        { value: 'shonen', label: '少年漫画' },
        { value: 'shoujo', label: '少女漫画' },
        { value: 'chibi', label: 'Q版漫画' }
      ],
      isGenerating: false,
      comicData: null,
      progress: 0,
      currentPanel: 0
    }
  },
  methods: {
    async generateComic() {
      if (!this.inputText.trim()) {
        alert('请输入故事内容');
        return;
      }

      this.isGenerating = true;
      this.progress = 0;
      this.currentPanel = 1;

      try {
        const response = await this.$http.post('/api/text-to-comic/six-panel', {
          text: this.inputText,
          style: this.selectedStyle
        });

        this.comicData = response.data;
        this.$store.dispatch('addToHistory', {
          type: 'six-panel',
          content: this.inputText,
          result: this.comicData
        });
      } catch (error) {
        console.error('生成漫画失败:', error);
        alert('生成六格漫画失败，请重试');
      } finally {
        this.isGenerating = false;
        this.progress = 100;
      }
    },

    downloadAll() {
      if (!this.comicData?.image_urls) return;

      this.comicData.image_urls.forEach((url, index) => {
        if (url) {
          const link = document.createElement('a');
          link.href = url;
          link.download = `漫画第${index+1}格.png`;
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        }
      });
    },

    regenerate() {
      if (this.comicData) {
        this.inputText = this.comicData.description;
        this.generateComic();
      }
    },

    updateProgress(panel) {
      this.currentPanel = panel;
      this.progress = Math.floor((panel / 6) * 100);
    }
  }
}
</script>

<style scoped>
.six-panel-comic {
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px;
}

h2 {
  text-align: center;
  margin-bottom: 20px;
  color: #333;
}

.input-section {
  margin-bottom: 30px;
}

textarea {
  width: 100%;
  height: 150px;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 16px;
  margin-bottom: 15px;
  resize: vertical;
}

.controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.style-selector {
  display: flex;
  align-items: center;
}

.style-selector label {
  margin-right: 10px;
  font-weight: bold;
}

.style-selector select {
  padding: 8px 12px;
  border-radius: 4px;
  border: 1px solid #ddd;
}

button {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

button:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

button:not(:disabled):hover {
  opacity: 0.9;
}

.comic-display {
  margin-top: 30px;
  border-top: 1px solid #eee;
  padding-top: 20px;
}

.comic-description {
  background-color: #f9f9f9;
  padding: 15px;
  border-radius: 6px;
  margin-bottom: 20px;
}

.comic-description h3 {
  margin-top: 0;
  color: #444;
}

.description-text p {
  margin: 8px 0;
  line-height: 1.6;
}

.comic-panels {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 15px;
  margin-bottom: 20px;
}

.panel {
  position: relative;
  border: 1px solid #ddd;
  border-radius: 6px;
  overflow: hidden;
  aspect-ratio: 3/4;
  background-color: #f5f5f5;
}

.panel-number {
  position: absolute;
  top: 5px;
  left: 5px;
  background-color: rgba(0,0,0,0.6);
  color: white;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 12px;
}

.panel img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.error-panel {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #ff4444;
}

.comic-actions {
  display: flex;
  justify-content: center;
  gap: 15px;
  margin-top: 20px;
}

.download-btn {
  background-color: #4CAF50;
  color: white;
}

.regenerate-btn {
  background-color: #2196F3;
  color: white;
}

.generation-progress {
  margin-top: 30px;
  text-align: center;
}

.progress-bar {
  width: 100%;
  height: 10px;
  background-color: #eee;
  border-radius: 5px;
  overflow: hidden;
  margin-bottom: 10px;
}

.progress {
  height: 100%;
  background-color: #4CAF50;
  transition: width 0.3s;
}

@media (max-width: 768px) {
  .comic-panels {
    grid-template-columns: repeat(2, 1fr);
  }

  .controls {
    flex-direction: column;
    gap: 15px;
    align-items: flex-start;
  }
}

@media (max-width: 480px) {
  .comic-panels {
    grid-template-columns: 1fr;
  }
}
</style>